/*
 * @Author: your name
 * @Date: 2021-05-08 09:51:10
 * @LastEditTime: 2021-05-12 12:23:24
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \after-end\app\middleware\auth.js
 */
const jwt = require("jsonwebtoken");

//白名单
const whiteList = [
  "/",
  "/api/addList",
  "/api/register",
  "/api/login",
  "/newHouseList",
  "/oldHouseList",
  "/rentingList",
  "/api/updaterouter",
  "/api/fandusertable"
];

module.exports = function () {
  return async function (ctx, next) {
    if (whiteList.includes(ctx.path)) {
      await next();
    } else {
      const { authorization } = ctx.request.headers;
      try {
        const data = jwt.verify(authorization, ctx.app.config.keys);
        ctx.state = data;
        await next();
      } catch (error) {
        ctx.body = {
          code: -1,
          message: "token验证失败",
          error,
        };
      }
    }
  };
};
